#include <bits/stdc++.h>
using namespace std;

void solve(){
    int n, m;
    cin >> n >> m;
    map<int, int> mp;
    int a, b;
    while(m--){
        cin >> a >> b;
        mp[a] += b;
    }
    priority_queue<int, vector<int>, less<int>> heap;
    for(auto [k, v] : mp){
        heap.push(v);
    }
    int ans = 0;
    while(n && heap.size()){
        ans += heap.top();
        heap.pop();
        n--;
    }
    cout << ans << endl;
}

int main(){
    int t;
    cin >> t;
    while(t --) solve();
    return 0;
}